﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using DTO; 
namespace BLL.Util
{
    public class CacheHelper
    {
        private static ICacheManager cacheManager = CacheFactory.GetCacheManager();//实例化ICachemanager;

        #region public function

        /// <summary>
        /// 缓存用户账号
        /// </summary>
        /// <param name="account"></param>
        public static void CacheAddAccount(Account account)
        {
            AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddMinutes(10));//指定10分钟后过期
            cacheManager.Add(account.ID.ToString(), account, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
        }
        /// <summary>
        /// 获取账号
        /// </summary>
        /// <param name="accountID"></param>
        /// <returns></returns>
        public static Account GetAccount(string accountID)
        {
            Account a = cacheManager.GetData(accountID) as Account;
            if (a == null)
            {
                var account = AccountBLL.GetAccount(accountID);
                CacheAddAccount(account);
                return account;
            }
            return a;
        }
        /// <summary>
        /// 获取我的账户
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public static List<Card> GetMyCard(string aid)
        {
            string key = MyCard + aid;

            List<Card> foo = cacheManager.GetData(key) as List<Card>;
            if (foo==null)
            {
                var bar = CardBLL.GetMyCardList(aid);
                AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddMinutes(2));//指定2分钟后过期
                cacheManager.Add(key, bar, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
                return bar;
            }
            return foo;
        }
        /// <summary>
        /// 获取我的所有记录情况
        /// </summary>
        /// <returns></returns>
        public static List<Record> GetMyAllRecord(string aid)
        {
            string key = MyRecord + aid;
            var foo = cacheManager.GetData(key) as List<Record>;
            if (foo == null)
            {
                var bar = RecordBLL.GetMyAllRecord(aid);
                AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddMinutes(2));//指定2分钟后过期
                var t =bar.ToList(); 
                cacheManager.Add(key, t, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
                return t;
            }
            return foo;
        }
        /// <summary>
        /// 返回当前用户的所有分类
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public static List<Category> GetMyAllCategory(string aid)
        {
            string key = MyCategory + aid;

            var foo = cacheManager.GetData(key) as List<Category>;
            if (foo == null)
            {
                var bar = CategoryBLL.GetMyAllCategory(aid);
                SlidingTime _ExpireTime = new SlidingTime(new TimeSpan(0, 5, 0));//指定5分钟后过期
                cacheManager.Add(key, bar, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
                return bar;
            }
            return foo;
        }
        /// <summary>
        /// 返回当前用户所有存折记录
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public static List<BankBook> GetMyAllBankBook(int aid)
        {
            string key = MyBankBook + aid;

            var foo = cacheManager.GetData(key) as List<BankBook>;
            if (foo == null)
            {
                foo = BankbookBLL.GetAllBankBook(aid);
                foreach (var item in foo)
                {
                    item.Card = CardBLL.GetMyCard(aid.ToString(), item.CardID.ToString());
                }
                SlidingTime _ExpireTime = new SlidingTime(new TimeSpan(0, 3, 0));//指定3分钟后过期
                cacheManager.Add(key, foo, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
            }
            return foo;
        }
        
        /// <summary>
        /// 获取所有组织架构信息
        /// </summary>
        /// <returns></returns>
        public static List<Organization> GetAllOrganization()
        {
            var foo = cacheManager.GetData(Organization) as List<Organization>;
            if (foo == null)
            {
                var bar = OrganizationBLL.GetAllOrganization();
                SlidingTime _ExpireTime = new SlidingTime(new TimeSpan(0,10,0));//指定10分钟后过期
                cacheManager.Add(Organization, bar, CacheItemPriority.High, null, _ExpireTime);//加入缓存
                return bar;
            }
            return foo;
        }
        /// <summary>
        /// 返回利率表
        /// </summary>
        /// <returns></returns>
        public static List<ConstRate> GetRate()
        {
            var foo = cacheManager.GetData(BankRate) as List<ConstRate>;
            if (foo == null)
            {
                var bar = Config.GetRateByDB();
                SlidingTime _ExpireTime = new SlidingTime(new TimeSpan(0, 10, 0));//指定10分钟后过期
                cacheManager.Add(Organization, bar, CacheItemPriority.High, null, _ExpireTime);//加入缓存
                return bar;
            }
            return foo;
        }

        #endregion


        #region remove cache
        public static void RemoveCache(string key, string aid)
        {
            cacheManager.Remove(key + aid);
        }

        public static void RemoveCache(string key)
        {
            cacheManager.Remove(key);
        }
        #endregion

        #region const string key
        /// <summary>
        /// 用户账户
        /// </summary>
        public const string MyCard = "mycard";
        /// <summary>
        /// 我的记录
        /// </summary>
        public const string MyRecord = "myreocrd";
        /// <summary>
        /// 用户消费类型
        /// </summary>
        public const string MyCategory = "myCategory";
        /// <summary>
        /// 组织架构
        /// </summary>
        private const string Organization = "Organization";
        /// <summary>
        /// 银行存折
        /// </summary>
        private const string MyBankBook = "bankbook";
        /// <summary>
        /// 银行利率
        /// </summary>
        private const string BankRate = "BankRate";

        #endregion

    }
}
